<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="http-客户端" tabindex="-1"><a class="header-anchor" href="#http-客户端"><span>HTTP 客户端</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#making-requests">发起请求</a>
<ul>
<li><a href="#request-data">请求数据</a></li>
<li><a href="#headers">请求头</a></li>
<li><a href="#authentication">认证</a></li>
<li><a href="#timeout">超时</a></li>
<li><a href="#retries">重试</a></li>
<li><a href="#error-handling">错误处理</a></li>
<li><a href="#guzzle-middleware">Guzzle 中间件</a></li>
<li><a href="#guzzle-options">Guzzle 选项</a></li>
</ul>
</li>
<li><a href="#concurrent-requests">并发请求</a></li>
<li><a href="#macros">宏</a></li>
<li><a href="#testing">测试</a>
<ul>
<li><a href="#faking-responses">模拟相应</a></li>
<li><a href="#inspecting-requests">注入请求</a></li>
<li><a href="#preventing-stray-requests">防止意外请求</a></li>
</ul>
</li>
<li><a href="#events">事件</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 为<a href="http://docs.guzzlephp.org/en/stable/" target="_blank" rel="noopener noreferrer">Guzzle HTTP 客户端</a>提供了一套语义化轻量的API，它可以让你快速的发起HTTP请求与其他Web应用程序进行通信。Laravel对Guzzle的常见用例进行了封装以提供良好的开发体验。</p>
<h2 id="创建请求" tabindex="-1"><a class="header-anchor" href="#创建请求"><span>创建请求</span></a></h2>
<p>你可以使用<code v-pre>Http</code>门面提供的<code v-pre>head</code>、<code v-pre>get</code>、<code v-pre>post</code>、<code v-pre>put</code>、<code v-pre>patch</code>和 <code v-pre>delete</code> 方法发起请求。首先，让我们看看如何发起一个基本的<code v-pre>GET</code>请求：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Http</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>get</code> 方法返回一个 <code v-pre>Illuminate\Http\Client\Response</code>的实例，该实例提供了多种方法来检查响应：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">body</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">string</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">json</span><span class="token punctuation">(</span><span class="token variable">$key</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">,</span> <span class="token variable">$default</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">mixed</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token keyword type-declaration">object</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">object</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">collect</span><span class="token punctuation">(</span><span class="token variable">$key</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token class-name class-name-fully-qualified return-type">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Collection</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">status</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">int</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">successful</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">failed</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">clientError</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">header</span><span class="token punctuation">(</span><span class="token variable">$header</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">string</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">headers</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">array</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>Illuminate\Http\Client\Response</code> 对象还实现了 PHP 的 <code v-pre>ArrayAccess</code> 接口，允许你直接在响应上访问 JSON 响应数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com/users/1'</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>除了上面列出的响应方法外，还可以使用以下方法来确定响应是否具有给定的状态代码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span>                  <span class="token comment">// 200 OK</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">created</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span>             <span class="token comment">// 201 Created</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">accepted</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span>            <span class="token comment">// 202 Accepted</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">noContent</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span>           <span class="token comment">// 204 No Content</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">movedPermanently</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span>    <span class="token comment">// 301 Moved Permanently</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">found</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span>               <span class="token comment">// 302 Found</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">badRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span>          <span class="token comment">// 400 Bad Request</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">unauthorized</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span>        <span class="token comment">// 401 Unauthorized</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">paymentRequired</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span>     <span class="token comment">// 402 Payment Required</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">forbidden</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span>           <span class="token comment">// 403 Forbidden</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">notFound</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span>            <span class="token comment">// 404 Not Found</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">requestTimeout</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span>      <span class="token comment">// 408 Request Timeout</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">conflict</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span>            <span class="token comment">// 409 Conflict</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">unprocessableEntity</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span> <span class="token comment">// 422 Unprocessable Entity</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">tooManyRequests</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span>     <span class="token comment">// 429 Too Many Requests</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">serverError</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token keyword return-type">bool</span><span class="token punctuation">;</span>         <span class="token comment">// 500 Internal Server Error</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="uri-模板" tabindex="-1"><a class="header-anchor" href="#uri-模板"><span>URI 模板</span></a></h4>
<p>HTTP客户端还允许你使用 <a href="https://www.rfc-editor.org/rfc/rfc6570" target="_blank" rel="noopener noreferrer">URI 模板规范</a>构建请求 URL。如果要通过 URI 模板展开 URL 参数，可以使用<code v-pre>withUrlParameters</code>方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">withUrlParameters</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'endpoint'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'https://laravel.com'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'page'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'docs'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'version'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'11.x'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'topic'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'validation'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'{+endpoint}/{page}/{version}/{topic}'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="打印请求信息" tabindex="-1"><a class="header-anchor" href="#打印请求信息"><span>打印请求信息</span></a></h4>
<p>如果要在发送请求之前打印输出请求信息并且结束脚本运行，你应该在创建请求前调用<code v-pre>dd</code>方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">dd</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="请求数据" tabindex="-1"><a class="header-anchor" href="#请求数据"><span>请求数据</span></a></h3>
<p>大多数情况下，<code v-pre>POST</code>、<code v-pre>PUT</code>和<code v-pre>PATCH</code> 携带着额外的请求数据是相当常见的。所以，这些方法的第二个参数接受一个包含着请求数据的数组。默认情况下，这些数据会使用<code v-pre>application/json</code>类型随请求发送：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Http</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com/users'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Steve'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'role'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Network Administrator'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="get-请求查询参数" tabindex="-1"><a class="header-anchor" href="#get-请求查询参数"><span>GET 请求查询参数</span></a></h4>
<p>在创建 <code v-pre>GET</code> 请求时，你可以通过直接向 URL 添加查询字符串或是将键值对作为第二个参数传递给 <code v-pre>get</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com/users'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'page'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，可以使用 <code v-pre>withQueryParameters</code> 方法:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">retry</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withQueryParameters</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'page'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com/users'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="发送-url-编码请求" tabindex="-1"><a class="header-anchor" href="#发送-url-编码请求"><span>发送 URL 编码请求</span></a></h4>
<p>如果你希望使用 <code v-pre>application/x-www-form-urlencoded</code> 作为请求的数据类型，你应该在创建请求前调用 <code v-pre>asForm</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">asForm</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com/users'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Sara'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'role'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Privacy Consultant'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="发送原始数据-raw-请求" tabindex="-1"><a class="header-anchor" href="#发送原始数据-raw-请求"><span>发送原始数据（Raw）请求</span></a></h4>
<p>如果你想使用一个原始请求体发送请求，你可以在创建请求前调用 <code v-pre>withBody</code> 方法。你还可以将数据类型可以通过该方法的第二个参数提供：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">withBody</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token function">base64_encode</span><span class="token punctuation">(</span><span class="token variable">$photo</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'image/jpeg'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com/photo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="multi-part-请求" tabindex="-1"><a class="header-anchor" href="#multi-part-请求"><span>Multi-Part 请求</span></a></h4>
<p>如果您希望以 Multi-Part 请求的形式发送文件，应在发起请求之前调用 <code v-pre>attach</code> 方法。此方法接受文件的名称和内容作为参数。如果需要，您可以提供第三个参数，这将被视为文件的文件名，而第四个参数可用于提供与文件相关的头信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">attach</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'attachment'</span><span class="token punctuation">,</span> <span class="token function">file_get_contents</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'photo.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'photo.jpg'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Content-Type'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'image/jpeg'</span><span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com/attachments'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>除了传递文件的原始内容，你也可以传递 Stream 流数据:</p>
<div class="language-PHP line-numbers-mode" data-highlighter="prismjs" data-ext="PHP" data-title="PHP"><pre v-pre class="language-PHP"><code><span class="line">    $photo = fopen('photo.jpg', 'r');</span>
<span class="line"></span>
<span class="line">    $response = Http::attach(</span>
<span class="line">        'attachment', $photo, 'photo.jpg'</span>
<span class="line">    )-&gt;post('http://example.com/attachments');</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="请求头" tabindex="-1"><a class="header-anchor" href="#请求头"><span>请求头</span></a></h3>
<p>可以使用 <code v-pre>withHeaders</code> 方法添加请求头, 该方法接受键/值对格式的数组：</p>
<div class="language-PHP line-numbers-mode" data-highlighter="prismjs" data-ext="PHP" data-title="PHP"><pre v-pre class="language-PHP"><code><span class="line">    $response = Http::withHeaders([</span>
<span class="line">        'X-First' =&gt; 'foo',</span>
<span class="line">        'X-Second' =&gt; 'bar'</span>
<span class="line">    ])-&gt;post('http://example.com/users', [</span>
<span class="line">        'name' =&gt; 'Taylor',</span>
<span class="line">    ]);</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>accept</code> 方法指定应用程序响应你的请求所需的内容类型</p>
<div class="language-PHP line-numbers-mode" data-highlighter="prismjs" data-ext="PHP" data-title="PHP"><pre v-pre class="language-PHP"><code><span class="line">    $response = Http::accept('application/json')-&gt;get('http://example.com/users');</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>为方便起见，您可以使用 <code v-pre>acceptJson</code> 方法快速指定你的应用程序需要 <code v-pre>application/json</code> 内容类型来响应你的请求:</p>
<div class="language-PHP line-numbers-mode" data-highlighter="prismjs" data-ext="PHP" data-title="PHP"><pre v-pre class="language-PHP"><code><span class="line">    $response = Http::acceptJson()-&gt;get('http://example.com/users');</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>withHeaders</code>方法将请求头项合并到请求的现有请求头中，如果需要，你可以使用 <code v-pre>replaceHeaders</code> 方法完全替换整个请求头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">withHeaders</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'X-Original'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">replaceHeaders</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'X-Replacement'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com/users'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="认证" tabindex="-1"><a class="header-anchor" href="#认证"><span>认证</span></a></h3>
<p>你可以分别使用 <code v-pre>withBasicAuth</code> 和 <code v-pre>withDigestAuth</code> 方法指定基本和摘要身份验证凭据:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token comment">// Basic 认证方式...</span></span>
<span class="line">    <span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">withBasicAuth</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor@laravel.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'secret'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// Digest 认证方式...</span></span>
<span class="line">    <span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">withDigestAuth</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor@laravel.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'secret'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="bearer-令牌" tabindex="-1"><a class="header-anchor" href="#bearer-令牌"><span>Bearer 令牌</span></a></h4>
<p>如果你想要为你的请求快速添加名为 <code v-pre>Authorization</code> 的 Token 令牌请求头，您可以使用 <code v-pre>withToken</code> 方法:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"></span>
<span class="line">    <span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">withToken</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'token'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="超时" tabindex="-1"><a class="header-anchor" href="#超时"><span>超时</span></a></h3>
<p>你可以使用<code v-pre>timeout</code>方法指定等待响应的最大秒数。默认情况下HTTP客户端将在30秒后超时：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">timeout</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果超过给定的超时时间，将会抛出 <code v-pre>Illuminate\Http\Client\ConnectionException</code>的错误.</p>
<p>你可以使用<code v-pre>connectTimeout</code>方法指定尝试连接服务器时等待的最大秒数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">connectTimeout</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="重试" tabindex="-1"><a class="header-anchor" href="#重试"><span>重试</span></a></h3>
<p>如果你想要 HTTP 客户端在发生客户端或服务器错误时自动重试请求，你可以使用<code v-pre>retry</code>方法。<code v-pre>retry</code>方法接受请求应该尝试的最大次数和 Laravel 在尝试之间应该等待的毫秒数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">retry</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你想要手动计算尝试之间睡眠的毫秒数，你可以将闭包作为第二个参数传递给 <code v-pre>retry</code>方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Exception</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">retry</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">int</span> <span class="token variable">$attempt</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Exception</span> <span class="token variable">$exception</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$attempt</span> <span class="token operator">*</span> <span class="token number">100</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>为了方便起见，你也可以为<code v-pre>retry</code>方法的第一个参数传入一个数组。这个数组将被用来决定在后续尝试之间睡眠多少毫秒：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">retry</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果需要的话，你可以传递一个第三个参数给<code v-pre>retry</code>方法。第三个参数应该是一个可调用的，用于决定是否真的应该尝试重试。例如，如果初始请求遇到了<code v-pre>ConnectionException</code>，你可能只希望重试请求：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Exception</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Client<span class="token punctuation">\</span>PendingRequest</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">retry</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Exception</span> <span class="token variable">$exception</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">PendingRequest</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$exception</span> <span class="token keyword">instanceof</span> <span class="token class-name">ConnectionException</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果一个请求尝试失败了，你可能希望在进行新尝试之前对请求做出改变。你可以通过修改提供给<code v-pre>retry</code>方法闭包的请求参数来实现这一点。例如，如果第一次尝试返回了一个认证错误，你可能希望用一个新的授权令牌重试请求：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Exception</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Client<span class="token punctuation">\</span>PendingRequest</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Client<span class="token punctuation">\</span>RequestException</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">withToken</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">getToken</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">retry</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Exception</span> <span class="token variable">$exception</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">PendingRequest</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span> <span class="token variable">$exception</span> <span class="token keyword">instanceof</span> <span class="token class-name">RequestException</span> <span class="token operator">||</span> <span class="token variable">$exception</span><span class="token operator">-></span><span class="token property">response</span><span class="token operator">-></span><span class="token function">status</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!==</span> <span class="token number">401</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token constant boolean">false</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">withToken</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">getNewToken</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果所有的请求都失败了，将会抛出一个<code v-pre>Illuminate\Http\Client\RequestException</code>错误。 如果你想要禁用这种行为，可以把<code v-pre>throw</code>设置为<code v-pre>false</code>。 当禁用时，客户端收到的最后一个响应将在所有重试尝试之后被返回：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">retry</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token keyword">throw</span><span class="token punctuation">:</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p>[!WARNING]<br>
如果所有的请求都因为连接问题失败了，即使把<code v-pre>throw</code>设置为<code v-pre>false</code>，依然会抛出 <code v-pre>Illuminate\Http\Client\ConnectionException</code>错误</p>
</blockquote>
<h3 id="错误处理" tabindex="-1"><a class="header-anchor" href="#错误处理"><span>错误处理</span></a></h3>
<p>与 Guzzle 的默认行为不同，Laravel 的 HTTP 客户端包装器在客户端或服务器错误（服务器返回<code v-pre>4xx</code>和<code v-pre>5xx</code>状态码）时不会抛出异常。你可以使用<code v-pre>successful</code>、<code v-pre>clientError</code>或者<code v-pre>serverError</code>方法来确定是否返回了这些错误之一：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 判断状态码是否 >= 200 且 &lt; 300...</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">successful</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 判断状态码是否 >= 400...</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">failed</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 判断响状态码是否是 4xx</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">clientError</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 判断响状态码是否是 5xx</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">serverError</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 如果发生了客户端或服务器错误，立即执行给定的回调...</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">onError</span><span class="token punctuation">(</span><span class="token keyword type-hint">callable</span> <span class="token variable">$callback</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="抛出异常" tabindex="-1"><a class="header-anchor" href="#抛出异常"><span>抛出异常</span></a></h4>
<p>如果你有一个响应实例，并希望如果响应状态码指示客户端或服务器错误时抛出一个 <code v-pre>Illuminate\Http\Client\RequestException</code> 实例，你可以使用 <code v-pre>throw</code> 或 <code v-pre>throwIf</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Client<span class="token punctuation">\</span>Response</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 如果客户端或服务器错误发生，抛出异常...</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">throw</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 如果错误发生并且给定条件为真，抛出异常..</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">throwIf</span><span class="token punctuation">(</span><span class="token variable">$condition</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 如果错误发生并且给定闭包解析为真，抛出异常...</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">throwIf</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Response</span> <span class="token variable">$response</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 如果错误发生并且给定条件为假，抛出异常...</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">throwUnless</span><span class="token punctuation">(</span><span class="token variable">$condition</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 如果错误发生并且给定闭包解析为假，抛出异常...</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">throwUnless</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Response</span> <span class="token variable">$response</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 如果响应有特定的状态码，抛出异常...</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">throwIfStatus</span><span class="token punctuation">(</span><span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 除非响应有特定的状态码，否则抛出异常...</span></span>
<span class="line"><span class="token variable">$response</span><span class="token operator">-></span><span class="token function">throwUnlessStatus</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token variable">$response</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'user'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>Illuminate\Http\Client\RequestException</code> 实例有一个公共的 <code v-pre>$response</code> 属性，该属性允许你检查返回的响应。</p>
<p>如果没有发生错误，<code v-pre>throw</code> 方法会返回响应实例，允许你将其他操作链式调用到 <code v-pre>throw</code> 方法上：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">throw</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">json</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你想要在抛出异常之前执行一些附加逻辑，你可以传递一个闭包给 <code v-pre>throw</code> 方法。闭包被调用后，异常将自动被抛出，所以你不需要在闭包内重新抛出异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Client<span class="token punctuation">\</span>Response</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Client<span class="token punctuation">\</span>RequestException</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">throw</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Response</span> <span class="token variable">$response</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">RequestException</span> <span class="token variable">$e</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">json</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="guzzle-中间件" tabindex="-1"><a class="header-anchor" href="#guzzle-中间件"><span>Guzzle 中间件</span></a></h3>
<p>由于 Laravel 的 HTTP 客户端由 Guzzle 提供支持，你可以利用 <a href="https://docs.guzzlephp.org/en/stable/handlers-and-middleware.html" target="_blank" rel="noopener noreferrer">Guzzle 中间件</a>来操作正在进行的请求或检查传入的响应。要操作正在进行的请求，通过 <code v-pre>withRequestMiddleware</code> 方法注册一个 Guzzle 中间件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Http</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Psr<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Message<span class="token punctuation">\</span>RequestInterface</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">withRequestMiddleware</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">RequestInterface</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">withHeader</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'X-Example'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>同样，你可以通过注册 <code v-pre>withResponseMiddleware</code> 方法的中间件来检查传入的 HTTP 响应：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Http</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Psr<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Message<span class="token punctuation">\</span>ResponseInterface</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">withResponseMiddleware</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">ResponseInterface</span> <span class="token variable">$response</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$header</span> <span class="token operator">=</span> <span class="token variable">$response</span><span class="token operator">-></span><span class="token function">getHeader</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'X-Example'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$response</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="全局中间件" tabindex="-1"><a class="header-anchor" href="#全局中间件"><span>全局中间件</span></a></h4>
<p>有时，你可能希望注册一个适用于每个正在进行的请求和传入响应的中间件。为此，你可以使用 <code v-pre>globalRequestMiddleware</code> 和 <code v-pre>globalResponseMiddleware</code> 方法。通常，这些方法应该在你的应用的 <code v-pre>AppServiceProvider</code> 的 <code v-pre>boot</code> 方法中调用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Http</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">globalRequestMiddleware</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">withHeader</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'User-Agent'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Example Application/1.0'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">globalResponseMiddleware</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token variable">$response</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$response</span><span class="token operator">-></span><span class="token function">withHeader</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token string single-quoted-string">'X-Finished-At'</span><span class="token punctuation">,</span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">toDateTimeString</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="guzzle-选项" tabindex="-1"><a class="header-anchor" href="#guzzle-选项"><span>Guzzle 选项</span></a></h3>
<p>你可以使用 <code v-pre>withOptions</code> 方法为正在进行的请求指定额外的 <a href="http://docs.guzzlephp.org/en/stable/request-options.html" target="_blank" rel="noopener noreferrer">Guzzle 请求选项</a>。<code v-pre>withOptions</code> 方法接受一个键值对数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">withOptions</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'debug'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com/users'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="全局选项" tabindex="-1"><a class="header-anchor" href="#全局选项"><span>全局选项</span></a></h4>
<p>要为每个正在进行的请求配置默认选项，你可以使用 <code v-pre>globalOptions</code> 方法。通常，该方法应该从你的应用的 <code v-pre>AppServiceProvider</code> 的 <code v-pre>boot</code> 方法中调用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Http</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 启动任何应用服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">globalOptions</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'allow_redirects'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="并发请求" tabindex="-1"><a class="header-anchor" href="#并发请求"><span>并发请求</span></a></h2>
<p>有时，你可能希望同时进行多个 HTTP 请求。换句话说，你希望同时派发几个请求，而不是顺序地发出请求。这在与慢速 HTTP API 交互时可以带来显著的性能提升。</p>
<p>幸运的是，你可以使用 <code v-pre>pool</code> 方法来实现这一点。<code v-pre>pool</code> 方法接受一个闭包，该闭包接收一个 <code v-pre>Illuminate\Http\Client\Pool</code> 实例，允许你轻松地向请求池添加请求以进行派发：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Client<span class="token punctuation">\</span>Pool</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Http</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$responses</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">pool</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Pool</span> <span class="token variable">$pool</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://localhost/first'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://localhost/second'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://localhost/third'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token variable">$responses</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">-></span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span></span>
<span class="line">       <span class="token variable">$responses</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">-></span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span></span>
<span class="line">       <span class="token variable">$responses</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token operator">-></span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如你所见，每个响应实例都可以根据它被加入池的顺序访问。如果你愿意，可以使用 <code v-pre>as</code> 方法为请求命名，这样可以按名称访问对应的响应：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Client<span class="token punctuation">\</span>Pool</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Http</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$responses</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">pool</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Pool</span> <span class="token variable">$pool</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">as</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'first'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://localhost/first'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">as</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'second'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://localhost/second'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">as</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'third'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://localhost/third'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token variable">$responses</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'first'</span><span class="token punctuation">]</span><span class="token operator">-></span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义并发请求" tabindex="-1"><a class="header-anchor" href="#自定义并发请求"><span>自定义并发请求</span></a></h4>
<p><code v-pre>pool</code> 方法不能与其他 HTTP 客户端方法（例如 <code v-pre>withHeaders</code> 或 <code v-pre>middleware</code> 方法）链式调用。如果你想要将自定义头或中间件应用于池中的请求，应该在池中的每个请求上配置这些选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Client<span class="token punctuation">\</span>Pool</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Http</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$headers</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'X-Example'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'example'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$responses</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">pool</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Pool</span> <span class="token variable">$pool</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">withHeaders</span><span class="token punctuation">(</span><span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://laravel.test/test'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">withHeaders</span><span class="token punctuation">(</span><span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://laravel.test/test'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token variable">$pool</span><span class="token operator">-></span><span class="token function">withHeaders</span><span class="token punctuation">(</span><span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://laravel.test/test'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="宏" tabindex="-1"><a class="header-anchor" href="#宏"><span>宏</span></a></h2>
<p>Laravel HTTP 客户端允许你定义「宏」，它可以作为一个流畅的、富有表现力的机制来配置常见的请求路径和头信息，当与整个应用中的服务交云时使用。开始使用宏，你可以在应用的 <code v-pre>App\Providers\AppServiceProvider</code> 类的 <code v-pre>boot</code> 方法中定义：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Http</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 启动任何应用服务。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">macro</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'github'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">withHeaders</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'X-Example'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'example'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">baseUrl</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'https://github.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦你的宏配置好了，你可以在应用中的任何地方调用它来创建一个带有指定配置的待处理请求：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">github</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="测试" tabindex="-1"><a class="header-anchor" href="#测试"><span>测试</span></a></h2>
<p>许多 Laravel 服务提供了帮助你轻松且有表现力地编写测试的功能，Laravel 的 HTTP 客户端也不例外。<code v-pre>Http</code> facade 的 <code v-pre>fake</code> 方法允许你指导 HTTP 客户端在请求时返回存根 / 虚拟响应。</p>
<h3 id="伪造响应" tabindex="-1"><a class="header-anchor" href="#伪造响应"><span>伪造响应</span></a></h3>
<p>例如，要指示 HTTP 客户端在每个请求中返回空的 <code v-pre>200</code> 状态码响应，你可以调用 <code v-pre>fake</code> 方法而不传递参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Http</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$response</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="伪造特定的-url" tabindex="-1"><a class="header-anchor" href="#伪造特定的-url"><span>伪造特定的 URL</span></a></h4>
<p>或者，你可以向 <code v-pre>fake</code> 方法传递一个数组。数组的键应该代表你希望伪造的 URL 模式及其关联的响应。<code v-pre>*</code> 字符可用作通配符。对于没有被伪造的 URL 发出的任何请求都会被实际执行。你可以使用 <code v-pre>Http</code> facade 的 <code v-pre>response</code> 方法为这些端点构建存根 / 虚拟响应：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// 为 GitHub 端点存根一个 JSON 响应...</span></span>
<span class="line">    <span class="token string single-quoted-string">'github.com/*'</span> <span class="token operator">=></span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">response</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'foo'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 为 Google 端点存根一个字符串响应...</span></span>
<span class="line">    <span class="token string single-quoted-string">'google.com/*'</span> <span class="token operator">=></span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">response</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想指定一个后备 URL 模式来存根所有不匹配的 URL，你可以使用单个 <code v-pre>*</code> 字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// 为 GitHub 端点存根 JSON 响应...</span></span>
<span class="line">    <span class="token string single-quoted-string">'github.com/*'</span> <span class="token operator">=></span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">response</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'foo'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Headers'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 为其他所有端点存根字符串响应...</span></span>
<span class="line">    <span class="token string single-quoted-string">'*'</span> <span class="token operator">=></span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">response</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Headers'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="伪造响应序列" tabindex="-1"><a class="header-anchor" href="#伪造响应序列"><span>伪造响应序列</span></a></h4>
<p>有时候，你可能需要为单个 URL 指定其一系列的伪造响应的返回顺序。你可以使用 <code v-pre>Http::sequence</code> 方法来构建响应，以实现这个功能：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// 存根 GitHub端点的一系列响应...</span></span>
<span class="line">    <span class="token string single-quoted-string">'github.com/*'</span> <span class="token operator">=></span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">sequence</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                            <span class="token operator">-></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span></span>
<span class="line">                            <span class="token operator">-></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'foo'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span></span>
<span class="line">                            <span class="token operator">-></span><span class="token function">pushStatus</span><span class="token punctuation">(</span><span class="token number">404</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当一个响应序列中的所有响应都被消费后，任何进一步的请求都会导致响应序列抛出异常。如果你想指定当序列为空时应该返回的默认响应，你可以使用 <code v-pre>whenEmpty</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// 为 GitHub 端点存根一系列响应...</span></span>
<span class="line">    <span class="token string single-quoted-string">'github.com/*'</span> <span class="token operator">=></span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">sequence</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                            <span class="token operator">-></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span></span>
<span class="line">                            <span class="token operator">-></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'foo'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span></span>
<span class="line">                            <span class="token operator">-></span><span class="token function">whenEmpty</span><span class="token punctuation">(</span><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">response</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想要伪造一个响应序列，但你又期望在伪造的时候无需指定一个特定的 URL 匹配模式，那么你可以使用 <code v-pre>Http::fakeSequence</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">fakeSequence</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">whenEmpty</span><span class="token punctuation">(</span><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">response</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="fake-回调" tabindex="-1"><a class="header-anchor" href="#fake-回调"><span>Fake 回调</span></a></h4>
<p>如果你需要更复杂的逻辑来确定对某些端点返回什么响应，你可以将一个闭包传递给 <code v-pre>fake</code> 方法。这个闭包将接收一个 <code v-pre>Illuminate\Http\Client\Request</code> 实例并应返回一个响应实例。在你的闭包中，你可以执行任何必要的逻辑来确定返回哪种类型的响应：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Client<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">response</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="避免「流浪的」请求" tabindex="-1"><a class="header-anchor" href="#避免「流浪的」请求"><span>避免「流浪的」请求</span></a></h3>
<p>如果你想确保通过 HTTP 客户端发送的所有请求都在你的个别测试或完整测试套件中被模拟过，你可以调用 <code v-pre>preventStrayRequests</code> 方法。调用这个方法后，任何没有相应假响应的请求都会抛出异常，而不是进行实际的 HTTP 请求：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Http</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">preventStrayRequests</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'github.com/*'</span> <span class="token operator">=></span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">response</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ok'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 将会返回 OK 响应...</span></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'https://github.com/laravel/framework'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 抛出一个异常...</span></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'https://laravel.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="检查请求" tabindex="-1"><a class="header-anchor" href="#检查请求"><span>检查请求</span></a></h3>
<p>当你伪造响应时，你可能偶尔希望检查客户端接收到的请求，以确保你的应用程序正在发送正确的数据或头部。你可以在调用 <code v-pre>Http::fake</code> 后通过调用 <code v-pre>Http::assertSent</code> 方法来完成这个操作。</p>
<p><code v-pre>assertSent</code> 方法接受一个闭包，该闭包会接收一个 <code v-pre>Illuminate\Http\Client\Request</code> 实例，并应返回一个布尔值，表明请求是否符合你的预期。为了使测试通过，至少必须发出一个与给定预期相匹配的请求：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Client<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Http</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">withHeaders</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'X-First'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com/users'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'role'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Developer'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">assertSent</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">hasHeader</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'X-First'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span></span>
<span class="line">           <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">url</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token string single-quoted-string">'http://example.com/users'</span> <span class="token operator">&amp;&amp;</span></span>
<span class="line">           <span class="token variable">$request</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string single-quoted-string">'Taylor'</span> <span class="token operator">&amp;&amp;</span></span>
<span class="line">           <span class="token variable">$request</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'role'</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string single-quoted-string">'Developer'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果需要，你可以使用 <code v-pre>assertNotSent</code> 方法来断言特定的请求没有被发送：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Client<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Http</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'http://example.com/users'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'role'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Developer'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">assertNotSent</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">url</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token string single-quoted-string">'http://example.com/posts'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>assertSentCount</code> 方法来断言在测试期间「发送」的请求数量：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">assertSentCount</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，你也可以使用 <code v-pre>assertNothingSent</code> 方法来断言在测试期间没有发送任何请求：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">assertNothingSent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="记录请求和响应" tabindex="-1"><a class="header-anchor" href="#记录请求和响应"><span>记录请求和响应</span></a></h4>
<p>你可以使用 <code v-pre>recorded</code> 方法来收集所有请求及其对应的响应。<code v-pre>recorded</code> 方法返回一个包含 <code v-pre>Illuminate\Http\Client\Request</code> 和 <code v-pre>Illuminate\Http\Client\Response</code> 实例的数组集合：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'https://laravel.com'</span> <span class="token operator">=></span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">response</span><span class="token punctuation">(</span><span class="token argument-name">status</span><span class="token punctuation">:</span> <span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'https://nova.laravel.com/'</span> <span class="token operator">=></span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">response</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'https://laravel.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'https://nova.laravel.com/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$recorded</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">recorded</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">[</span><span class="token variable">$request</span><span class="token punctuation">,</span> <span class="token variable">$response</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token variable">$recorded</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此外，<code v-pre>recorded</code> 方法接受一个闭包，该闭包会接收一个 <code v-pre>Illuminate\Http\Client\Request</code> 实例和一个 <code v-pre>Illuminate\Http\Client\Response</code> 实例，并且可以用来根据你的预期过滤请求 / 响应对：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Client<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Client<span class="token punctuation">\</span>Response</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'https://laravel.com'</span> <span class="token operator">=></span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">response</span><span class="token punctuation">(</span><span class="token argument-name">status</span><span class="token punctuation">:</span> <span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'https://nova.laravel.com/'</span> <span class="token operator">=></span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">response</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'https://laravel.com'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'https://nova.laravel.com/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$recorded</span> <span class="token operator">=</span> <span class="token class-name static-context">Http</span><span class="token operator">::</span><span class="token function">recorded</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Response</span> <span class="token variable">$response</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">url</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!==</span> <span class="token string single-quoted-string">'https://laravel.com'</span> <span class="token operator">&amp;&amp;</span></span>
<span class="line">           <span class="token variable">$response</span><span class="token operator">-></span><span class="token function">successful</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="事件" tabindex="-1"><a class="header-anchor" href="#事件"><span>事件</span></a></h2>
<p>在发送 HTTP 请求的过程中，Laravel 会触发三个事件。<code v-pre>RequestSending</code> 事件在请求发送之前触发，而 <code v-pre>ResponseReceived</code> 事件在接收到给定请求的响应后触发。如果没有收到给定请求的响应，则会触发 <code v-pre>ConnectionFailed</code> 事件。</p>
<p><code v-pre>RequestSending</code> 和 <code v-pre>ConnectionFailed</code> 事件都包含一个公共的 <code v-pre>$request</code> 属性，你可以使用它来检查 <code v-pre>Illuminate\Http\Client\Request</code> 实例。同样地，<code v-pre>ResponseReceived</code> 事件包含一个 <code v-pre>$request</code> 属性以及一个 <code v-pre>$response</code> 属性，可用于检查 <code v-pre>Illuminate\Http\Client\Response</code> 实例。你可以为你的应用程序中的这些事件创建<a href="https://learnku.com/docs/laravel/11.x/eventsmd" target="_blank" rel="noopener noreferrer">事件监听器</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Client<span class="token punctuation">\</span>Events<span class="token punctuation">\</span>RequestSending</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">LogRequest</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 处理给定的事件。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token class-name type-declaration">RequestSending</span> <span class="token variable">$event</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// $event->request ...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/http-clientmd/16680" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/ht...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/http-clientmd/16680" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/ht...</a></p>
</blockquote>
</div></template>


